package cn.vortex.device.web.servlet;

import cn.vortex.device.dao.impl.ApplyDaoImpl;
import cn.vortex.device.dao.impl.DeviceDaoImpl;
import cn.vortex.device.dao.impl.DownItemDaoImpl;
import cn.vortex.device.dao.impl.FixInfoDaoImpl;
import cn.vortex.device.vo.ResMessage;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

@WebServlet(name = "ConsoleServlet",urlPatterns = "/console/*")
public class ConsoleServlet extends BaseServlet {

    private DeviceDaoImpl deviceDao = new DeviceDaoImpl();
    private ApplyDaoImpl applyDao = new ApplyDaoImpl();
    private DownItemDaoImpl downItemDao = new DownItemDaoImpl();
    private FixInfoDaoImpl fixInfoDao = new FixInfoDaoImpl();

    public ResMessage getElemNums(HttpServletRequest request, HttpServletResponse response){
        ResMessage resMessage = null;//声明消息对象
        List<Integer> list = new ArrayList<>();//创建list链表用于存储信息
        try {
            int deviceCount = deviceDao.findCount("select count(1) from t_device where state != ?", 2);//获取现有设备数量
            int applyCount = applyDao.findCount("select count(1) from t_apply where isShow = ?",1);//获取申请设备数量
            int fixCount = deviceDao.findCount("select count(1) from t_device where state = ?", 1);//获取正在维修的设备数量
            int downItemCount = downItemDao.findCount("select count(1) from t_downItem");//获取已经报废的设备数量
            //将需要的信息添加到list中
            list.add(deviceCount);
            list.add(fixCount);
            list.add(downItemCount);
            list.add(applyCount);
            resMessage = new ResMessage(200,"success",list);//用list对返回信息对象进行初始化
        } catch (Exception e) {
            e.printStackTrace();
            return new ResMessage(200,"fail","系统错误");
        }
        return resMessage;//返回信息对象
    }


    public ResMessage getPieData(HttpServletRequest request,HttpServletResponse response){
        ResMessage resMessage = null;
        try {
            List<Map<String, Object>> pieData = deviceDao.getPieData();

            for (Map<String, Object> pd : pieData) {
                Set<Map.Entry<String, Object>> entrySet = pd.entrySet();
                Iterator<Map.Entry<String, Object>> iterator = entrySet.iterator();
                while (iterator.hasNext()){
                    Map.Entry<String, Object> entry = iterator.next();
                    System.out.println(entry.getKey() + "--->" + entry.getValue());
                }
            }

            resMessage = new ResMessage(200,"success",pieData);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResMessage(200,"fail","系统错误");
        }
        return resMessage;
    }

    public ResMessage getDeviceValueData(HttpServletRequest request,HttpServletResponse response){
        ResMessage resMessage = null;

        try {
            List<Map<String, Object>> data = deviceDao.getDeviceValueData();

            for (Map<String, Object> pd : data) {
                Set<Map.Entry<String, Object>> entrySet = pd.entrySet();
                Iterator<Map.Entry<String, Object>> iterator = entrySet.iterator();
                while (iterator.hasNext()){
                    Map.Entry<String, Object> entry = iterator.next();
                    System.out.println(entry.getKey() + "--->" + entry.getValue());
                }
            }

            resMessage = new ResMessage(200,"success",data);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResMessage(200,"fail","系统错误");
        }

        return resMessage;
    }

    public ResMessage getDeviceIO(HttpServletRequest request,HttpServletResponse response){
        ResMessage resMessage = null;
        List<Integer> data = new ArrayList();
        try {
            int deviceAmount = deviceDao.findCount("select sum(price) as amount from t_device where state != 2");
            int fixAmount = fixInfoDao.findCount("select sum(fixPrice) as amount from t_fixInfo");
            int applyAmount = applyDao.findCount("select sum(amount) as amount from t_apply where isShow = 0");
            //MySQL
            //int downAmount = downItemDao.findCount("select sum(price) as amount from t_downItem");
            //Oracle
            int downAmount = downItemDao.findCount("select sum(t_device.price) as amount " +
                    "from t_downitem, t_device where t_downitem.\"number\" = t_device.\"number\"");


            data.add(deviceAmount);
            data.add(fixAmount);
            data.add(applyAmount);
            data.add(downAmount);
            resMessage = new ResMessage(200,"success",data);
        } catch (Exception e) {
            e.printStackTrace();
          return new ResMessage(200,"fail","系统错误");
        }
        return resMessage;
    }


}
